package gggameserver.games.khet;

import gggameserver.games.Position;

public class KhetPiece {

	// direction for Pharaon, Anubis, Sphinx
	public static final int NORTH = 0;
	public static final int EAST = 1;
	public static final int SOUTH = 2;
	public static final int WEST = 3;

	// direction for pyramid, Scarab
	public static final int NORTH_EAST = NORTH;
	public static final int SOUTH_EAST = EAST;
	public static final int SOUTH_WEST = SOUTH;
	public static final int NORTH_WEST = WEST;

	public enum Type {
		PYRAMID, PHARAON, ANUBIS, SCARAB, SPHINX
	};

	private int player;
	private Type type;
	private boolean alive;
	private int direction;
	private Position pos;

	public KhetPiece(int player, Type type, int x, int y, int direction) {
		this.player = player;
		this.type = type;
		this.alive = true;
		this.direction = direction;
		this.pos = new Position(x, y);
		check();
	}

	public int getX() {
		return pos.getX();
	};

	public int getY() {
		return pos.getY();
	};

	public KhetPiece mirror() {
		Position mirrorPos = KhetBoard.mirrorPos(this.pos);
		return new KhetPiece(KhetBoard.getOponent(this.player), this.type,
				mirrorPos.getX(), mirrorPos.getY(),
				KhetBoard.mirrorDirection(this.direction));
	}

	public void check() {
		if (direction < 0 || 3 < direction)
			throw new IllegalStateException("Illegal KhetPiece direction "
					+ direction); // TODO
	}

	public int getPlayerNumber() {
		return this.player;
	}

	public int getDirection() {
		return this.direction;
	}

	public Type getType() {
		return this.type;
	}

	public boolean isAlive() {
		return this.alive;
	}

	public final Position getPosition() {
		return this.pos;
	}

	@Override
	public String toString() {
		return "KhetPiece [player=" + player + ", type=" + type + ", alive="
				+ alive + ", direction=" + direction + ", pos=" + pos + "]";
	}

	public void setDirection(int newDirection) {
		this.direction = newDirection;
		check();
	}

	public void setPosition(Position newPosition) {
		this.pos.set(newPosition);
		check();
	}

	public void setDestroyed() {
		this.alive = false;
	}

	public KhetPiece clonePiece() {
		KhetPiece piece = new KhetPiece(this.player, this.type,
				this.pos.getX(), this.pos.getY(), this.direction);
		piece.alive = this.alive;
		return piece;
	}

}
